<template>
  <div class="space-Rank-analyze">
    <a-card title="图库排名分析">
      <v-chart :option="options" style="height: 400px;width: 80%;" :loading="loading" />
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { listSpaceRankAnalyzeUsingPost } from '@/api/spaceAnalysisController.ts'
import VChart from 'vue-echarts'
import 'echarts'

interface Props {
  queryPublic?: boolean
  queryAll?: boolean
  spaceId?: number | string
}

const props = withDefaults(defineProps<Props>(), {
  queryPublic: false,
  queryAll: false,
})

// 获取使用情况
const dataList = ref<API.Space[]>([])

const loading = ref(false)
const getDataList = async () => {
  loading.value = true

  const res = await listSpaceRankAnalyzeUsingPost({
    topN: 10,
  })
  if (res.data.code === 0 && res.data.data) {
    dataList.value = res.data.data ?? []
  }
  loading.value = false
}

// 定义option
const options = computed(() => {
  const spaceNames = dataList.value.map((item) => item.spaceName)
  const usageData = dataList.value.map((item) => (item.totalSize / (1024 * 1024)).toFixed(2)) // 转为 MB

  return {
    tooltip: { trigger: 'axis' },
    xAxis: {
      type: 'category',
      data: spaceNames,
    },
    yAxis: {
      type: 'value',
      name: '空间使用量 (MB)',
    },
    series: [
      {
        name: '空间使用量 (MB)',
        type: 'bar',
        data: usageData,
        itemStyle: {
          color: '#5470C6', // 自定义柱状图颜色
        },
      },
    ],
  }
})


onMounted(() => {
  getDataList()
})
</script>

<style scoped lang="scss"></style>
